package com.yzy.event;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;

import java.util.Map;

/**
 * @author yangzhenyu
 * @version 1.0
 * @description:
 * @date 2022/7/14 10:17
 */
public abstract  class MyMessageListener implements ChannelAwareMessageListener {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Value("${ccbscf.application.com:true}")
    Boolean com;

    public MyMessageListener() {
    }

    public void onMessage(Message message, Channel channel) throws Exception {
        Map<String, Object> headers = message.getMessageProperties().getHeaders();
        if (!this.com) {
            if (!CollectionUtils.isEmpty(headers)) {
                if (!MqMessageHeaderProcessor.type.equals(headers.get("senderType"))) {
                    this.logger.warn("此消息不能被当前实例处理: 发送方:{}, 发送实例:{}, 发送实例类型:{}, 当前实例类型:{}", new Object[]{headers.get("msgSender"), headers.get("msgSendFrom"), headers.get("senderType"), MqMessageHeaderProcessor.type});
                    channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
                    return;
                }
            } else {
                this.logger.warn("ATTENTION: mq message header not exists ");
            }
        }



        if (headers.containsKey("channel_tag")) {
            this.logger.info("消费者onMessage当前线程号[{}]", Thread.currentThread().getId());
            this.logger.info("消费者onMessage,header info [{}]", JSON.toJSONString(headers));
        }

        this.processMessage(message, channel);
    }

    public abstract void processMessage(Message var1, Channel var2);
}
